package com.btpj.hospital_server.controller;

import com.btpj.hospital_server.dto.CommonResult;
import com.btpj.hospital_server.dto.param.UserMedicalCardParam;
import com.btpj.hospital_server.entity.UserMedicalCard;
import com.btpj.hospital_server.service.UserMedicalCardService;
import com.btpj.hospital_server.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "就诊卡信息接口")
@RestController
@RequestMapping("/card")
public class UserMedicalCardController {

    /**
     * 就诊卡最大绑定数量
     */
    private static final int MAX_CARD_NUMBER = 6;

    @Resource
    private UserMedicalCardService userMedicalCardService;

    @ApiOperation("获取就诊卡列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "就诊卡用户姓名", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "phone", value = "就诊卡用户手机号", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "gender", value = "就诊卡性别男1女2", paramType = "query", dataType = "Int")
    })
    @GetMapping("/list")
    public CommonResult<List<UserMedicalCard>> getCardList(@RequestParam(required = false) String name,
                                                           @RequestParam(required = false) String phone,
                                                           @RequestParam(required = false) Integer gender) {
        if (gender != null && !(gender == 1 || gender == 2)) {
            return CommonResult.error("性别参数错误");
        }

        return CommonResult.success(userMedicalCardService.getCardList(name, phone, gender));
    }

    @ApiOperation("获取当前用户已绑定的就诊卡列表")
    @GetMapping("/myCardList")
    public CommonResult<List<UserMedicalCard>> getMyCardList() {
        return CommonResult.success(userMedicalCardService.getMyCardList());
    }

    @ApiOperation("添加就诊卡信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "就诊卡用户姓名", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "phone", value = "就诊卡用户手机号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "gender", value = "就诊卡性别男1女2", required = true, paramType = "query", dataType = "Int"),
            @ApiImplicitParam(name = "identificationNumber", value = "证件号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "birthDate", value = "出生日期", required = true, paramType = "query", dataType = "Date"),
            @ApiImplicitParam(name = "type", value = "关系类型 0：本人，1：父母，2：兄弟/姐妹，3：伴侣，4：子女，5：同事/朋友，6：其他", required = true, paramType = "query", dataType = "Int")
    })
    @PostMapping("/add")
    public CommonResult<Void> addCard(UserMedicalCardParam param) {
        if (userMedicalCardService.getCardCountByAccountId() > MAX_CARD_NUMBER) {
            return CommonResult.error("就诊卡数量不可超过6张");
        }

        if (StringUtil.isEmpty(param.getName())) {
            return CommonResult.error("就诊卡用户姓名不能为空");
        }

        if (StringUtil.isEmpty(param.getPhone())) {
            return CommonResult.error("就诊卡用户手机号不能为空");
        }

        if (param.getGender() == null || !(param.getGender() == 1 || param.getGender() == 2)) {
            return CommonResult.error("就诊卡用户性别不能为空或性别参数传递错误");
        }

        if (StringUtil.isEmpty(param.getIdentificationNumber())) {
            return CommonResult.error("就诊卡用户证件号不能为空");
        }

        if (userMedicalCardService.exist(param.getIdentificationNumber())) {
            return CommonResult.error("已绑定该就诊卡");
        }

        if (param.getBirthDate() == null) {
            return CommonResult.error("就诊卡用户出生日期不能为空");
        }

        if (param.getType() == null || (param.getType() < 0 || param.getType() > 6)) {
            return CommonResult.error("就诊卡用户关系类型不能为空或用户关系类型参数有误");
        }

        if (userMedicalCardService.addCard(param)) {
            return CommonResult.success("添加就诊卡成功");
        }

        return CommonResult.error("服务器出错，添加就诊卡失败");
    }

    @ApiOperation("修改就诊卡信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cardId", value = "就诊卡编号", required = true, paramType = "path", dataType = "Long"),
            @ApiImplicitParam(name = "name", value = "就诊卡用户姓名", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "phone", value = "就诊卡用户手机号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "gender", value = "就诊卡性别男1女2", required = true, paramType = "query", dataType = "Int"),
            @ApiImplicitParam(name = "identificationNumber", value = "证件号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "birthDay", value = "出生日期", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "type", value = "关系类型 0：本人，1：父母，2：兄弟/姐妹，3：伴侣，4：子女，5：同事/朋友，6：其他", required = true, paramType = "query", dataType = "Int")
    })
    @PutMapping("/update/{cardId}")
    public CommonResult<Void> updateCard(@PathVariable Long cardId, UserMedicalCardParam param) {
        if (!userMedicalCardService.exist(cardId)) {
            return CommonResult.error("不存在该就诊卡编号");
        }

        if (StringUtil.isEmpty(param.getName())) {
            return CommonResult.error("就诊卡用户姓名不能为空");
        }

        if (StringUtil.isEmpty(param.getPhone())) {
            return CommonResult.error("就诊卡用户手机号不能为空");
        }

        if (param.getGender() == null || !(param.getGender() == 1 || param.getGender() == 2)) {
            return CommonResult.error("就诊卡用户性别不能为空或性别参数传递错误");
        }

        if (StringUtil.isEmpty(param.getIdentificationNumber())) {
            return CommonResult.error("就诊卡用户证件号不能为空");
        }

        if (userMedicalCardService.exist(param.getIdentificationNumber())) {
            return CommonResult.error("修改后的就诊卡证件号已存在，请先绑定该证件号后再作修改");
        }

        if (StringUtil.isEmpty(param.getName())) {
            return CommonResult.error("就诊卡用户出生日期不能为空");
        }

        if (param.getType() == null || (param.getType() < 0 || param.getType() > 6)) {
            return CommonResult.error("就诊卡用户关系类型不能为空或用户关系类型参数有误");
        }

        if (userMedicalCardService.updateCard(cardId, param)) {
            return CommonResult.success("修改就诊卡信息成功");
        }

        return CommonResult.error("服务器出错，修改就诊卡信息失败");
    }

    @ApiOperation("获取就诊卡信息")
    @ApiImplicitParam(name = "cardId", value = "就诊卡编号", required = true, paramType = "path", dataType = "Long")
    @GetMapping("/detail/{cardId}")
    public CommonResult<UserMedicalCard> getCard(@PathVariable Long cardId) {
        if (!userMedicalCardService.exist(cardId)) {
            return CommonResult.error("不存在该就诊卡编号");
        }

        UserMedicalCard card = userMedicalCardService.getCardDetail(cardId);

        if (card != null) {
            return CommonResult.success(card);
        }

        return CommonResult.error("服务器出错，获取就诊卡信息失败");
    }

    @ApiOperation("删除就诊卡")
    @ApiImplicitParam(name = "cardId", value = "就诊卡编号", required = true, paramType = "path", dataType = "Long")
    @DeleteMapping("/delete/{cardId}")
    public CommonResult<Void> deleteCard(@PathVariable Long cardId) {
        if (!userMedicalCardService.exist(cardId)) {
            return CommonResult.error("不存在该就诊卡编号");
        }

        if (userMedicalCardService.existRelation(cardId)) {
            return CommonResult.error("您没有绑定当前就诊卡,无权修改");
        }

        if (userMedicalCardService.deleteCard(cardId)) {
            return CommonResult.success("删除就诊卡成功");
        }

        return CommonResult.error("服务器出错，删除就诊卡失败");
    }

    @ApiOperation("检查就诊卡数目是否超过限制")
    @GetMapping("/checkCardNumOverLimit")
    public CommonResult<Boolean> checkCardNumOverLimit() {
        return CommonResult.success(userMedicalCardService.getCardCountByAccountId() > MAX_CARD_NUMBER);
    }

    @ApiOperation("检查就诊卡信息是否存在")
    @ApiImplicitParam(name = "identificationNumber", value = "证件号", required = true, paramType = "query", dataType = "String")
    @GetMapping("/checkIdentificationNumberExist")
    public CommonResult<Boolean> checkIdentificationNumberExist(String identificationNumber) {
        return CommonResult.success(userMedicalCardService.exist(identificationNumber));
    }
}
